<?php
 class Users extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    /**
     * Login by user name and password, this function check login 
     * and SAVE USER to SESSION
     * @author tien.nguyen
     * @return boolean
     */
    public function login($username, $password){
        $user = $this->getUserByUsernamePassword($username, $password);        
        if($user){
            
            if($password == $user['Password']){
                //save user login  to session user.
                $this->session->set_userdata($user);
                
                //return boolean
                return true;
            }
        }        
        return false;
    }
    
    /**
     * Change password
     * @author tien.nguyen
     */
    public function changePassword($oldPass, $newPass){ 
        $user = $this->getUserByUsernamePassword($this->session->userdata['UserName'], $oldPass);
        if($user){
            //update password
            $data['Password'] = $newPass;
            $res = $this->db->update('users', $data, array('UserId' => $user['UserId'])); 
            if($res){
                return array('save' => 1, 'message' => 'Lưu thành công.');
            } 
        }else{
            return array('save' => 0, 'message' => 'Mật khẩu củ không đúng.');
        }    
    }
    
    /**
     * Registry user name
     * @author tien.nguyen
     * @return boolean
     */
    public function registry($data){
        return $this->db->insert('users', $data);
    }
    
    /**
     * Update date
     * @author tien.nguyen
     */
    public function edit($data, $userId){
        $data = $this->_convertData($data);
        return $this->db->update('users', $data, array('UserId' => $userId));
    }
    
    /**
     * Convert date
     * @author tien.nguyen
     */
    private function _convertData($data){
       $res = array();
       $arr = array('UserId', 'FullName', 'Address', 'Phone', 'Email');
       foreach($arr as $item){
            if(isset($data[$item])){
                $res[$item] = $data[$item];
            }
       } 
       return $res;
    }
    
    /**
     * get user by username 
     * @author tien.nguyen
     */
    public function getUserByUsername($username){
        $query = $this->db->get_where('users', 'users.UserName = '. $this->db->escape($username));
        return $query->row();
    }
    
    /**
     * Get user by email 
     * @author tien.nguyen
     */
    public function getUserByEmail($email){
        $query = $this->db->get_where('users', 'users.Email = '. $this->db->escape($email));
        return $query->row();
    }
    
    public function getUserByUserId($userId){
         $query = $this->db->get_where('users', 'users.UserId = '. $this->db->escape($userId));
        return $query->row_array();
    }
    
    /**
     * Get user name and password
     * @author tien.nguyen
     * @return array object
     */
    public function getUserByUsernamePassword($username, $password){
        $query = $this->db->get_where('users','UserName = '. 
            $this->db->escape($username).' and Password = '. 
            $this->db->escape($password));
        return $query->row_array();    
    }
 }
?>